package cn.getech.data.development.service.impl;

import cn.getech.data.development.config.properties.BdpJobConfig;
import cn.getech.data.development.config.properties.DataDevelopmentConfig;
import cn.getech.data.development.constant.ConnectTypeEnum;
import cn.getech.data.development.constant.Constant;
import cn.getech.data.development.constant.VersionType;
import cn.getech.data.development.constant.WorkFlowMenuType;
import cn.getech.data.development.dto.IdAndNameDto;
import cn.getech.data.development.dto.JobInfoDto;
import cn.getech.data.development.dto.JobNodeConfigDto;
import cn.getech.data.development.dto.JobNodeLayConfigDto;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.entity.flink.BdpRealTableField;
import cn.getech.data.development.entity.flink.BdpRealTableInfo;
import cn.getech.data.development.entity.flink.FlinkNodeUnion;
import cn.getech.data.development.mapper.*;
import cn.getech.data.development.mapper.flink.BdpRealTableFieldMapper;
import cn.getech.data.development.mapper.flink.BdpRealTableInfoMapper;
import cn.getech.data.development.mapper.flink.FlinkNodeUnionMapper;
import cn.getech.data.development.model.dto.*;
import cn.getech.data.development.model.qo.BdpVersionQO;
import cn.getech.data.development.model.vo.RealTimeTaskMenuSearchVO;
import cn.getech.data.development.model.vo.RealTimeTaskMenuVO;
import cn.getech.data.development.model.vo.RealTimeTaskVO;
import cn.getech.data.development.service.*;
import cn.getech.data.development.utils.CompressUtil;
import cn.getech.data.development.utils.HdfsUtil;
import cn.getech.data.development.utils.JobNodeConfigUtil;
import cn.getech.data.development.utils.StrUtils;
import cn.getech.data.development.utils.spring.SpringContextUtil;
import cn.getech.data.development.utils.spring.SpringIpAndHostUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.QueryBean;
import cn.getech.system.center.model.res.UserListRes;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.aspectj.util.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 打包的版本 服务实现类
 * </p>
 *
 * @author hujz
 * @since 2020-06-30
 */
@Slf4j
@Service
public class BdpVersionServiceImpl extends ServiceImpl<BdpVersionMapper, BdpVersion> implements IBdpVersionService {

    @Autowired
    private BdpVersionMapper bdpVersionMapper;
    @Autowired
    private BdpVersionProcRelMapper bdpVersionProcRelMapper;
    @Autowired
    private ProcConnectMapper procConnectMapper;
    @Autowired
    private ProcTableMapper procTableMapper;
    @Autowired
    private JobRunUdfMapper jobRunUdfMapper;
    @Autowired
    private WorkMenuMapper workMenuMapper;
    @Autowired
    private IWorkMenuService iworkMenuService;
    @Autowired
    RealTimeTaskMenuService realTimeTaskMenuService;
    @Autowired
    private ProcInfoMapper procInfoMapper;
    @Autowired
    private WorkMenuJobRelMapper workMenuJobRelMapper;
    @Autowired
    private JobInfoMapper jobInfoMapper;
    @Autowired
    private JobNodeInfoMapper jobNodeInfoMapper;
    @Autowired
    private JobNodeConfMapper jobNodeConfMapper;
    @Autowired
    private JobNodeInfoService jobNodeInfoService;
    @Autowired
    private JobNodeConfService jobNodeConfService;
    @Autowired
    private WorkFlowDepMapper workFlowDepMapper;
    @Autowired
    private JobLinkMapper jobLinkMapper;
    @Autowired
    private BdpJobConfig bdpJobConfig;
    @Autowired
    private ConnectFieldInfoMapper connectFieldInfoMapper;
    @Autowired
    private ConfConnectMapper confConnectMapper;
    @Autowired
    private SapConnectFieldMapper sapConnectFieldMapper;
    @Autowired
    private TableInfoMapper tableInfoMapper;
    @Autowired
    private TableFieldInfoMapper tableFieldInfoMapper;
    @Autowired
    private BdpRealTableInfoMapper realTableInfoMapper;
    @Autowired
    private BdpRealTableFieldMapper realTableFieldMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DataDevelopmentConfig dataDevelopmentConfig;
    @Autowired
    private AlarmObjectService alarmObjectService;

    @Autowired
    private RealTimeTaskService realTimeTaskService;
    @Autowired
    private FlinkNodeUnionMapper flinkNodeUnionMapper;
    @Autowired
    BdpRealTaskAlarmObjectMapper realTaskAlarmObjectMapper;


    @Override
    public PageUtils page(BdpVersionQO versionQO) {
//        //调用 sys-center 服务 获取当前用户下的用户信息
//        Map<String,Object> paramMap=new HashMap<>();
//        paramMap.put("userId",versionQO.getUserId());
//        paramMap.put("tenantId", ShiroUtils.getTenantId());
//        List<UserListRes> userListRes = sysUserService.queryByName(paramMap);
//        List<Long> userIdList=null;
//        if(!CollectionUtils.isEmpty(userListRes)){
//            userIdList= userListRes.stream().map(UserListRes::getUserId).collect(Collectors.toList());
//            //把自己创建的加进去
//            if(!userIdList.contains(versionQO.getUserId())){
//                userIdList.add(versionQO.getUserId());
//            }
//        }
//        if(CollectionUtils.isEmpty(userIdList)){
//            userIdList=new ArrayList<>();
//            userIdList.add(versionQO.getUserId());
//        }
//        versionQO.setUserIdList(userIdList);
        //字符串特殊处理
        String versionName = versionQO.getVersionName();
        versionQO.setVersionName(StrUtils.escapeChar(versionName));
        String versionNum = versionQO.getVersionNum();
        versionQO.setVersionNum(StrUtils.escapeChar(versionNum));
//        versionQO.setVersionType(VersionType.PACK.getCode());
        if(Objects.equals(VersionType.BACKUPS.getCode(),versionQO.getVersionType())){
            //备份的没有租户的概念
            versionQO.setTenantId(null);
        }

        IPage<BdpVersion> page = bdpVersionMapper.selectListPage((Page<BdpVersion>) new QueryBean<BdpVersion, BdpVersionQO>().getPage(versionQO), versionQO);
        return new PageUtils(page);
    }

    @Override
    public void deleteById(Long id) {
        BdpVersion bdpVersion = bdpVersionMapper.selectById(id);
        if (null != bdpVersion) {
            bdpVersionMapper.deleteById(id);
            BdpVersionProcRel bdpVersionProcRel = bdpVersionProcRelMapper.selectOne(new QueryWrapper<BdpVersionProcRel>().eq("version_id", id));
            if (null != bdpVersionProcRel) {
                bdpVersionProcRelMapper.delete(new QueryWrapper<BdpVersionProcRel>().eq("version_id", id));
                //同时删除对应的hdfs上的文件
                HdfsUtil hdfsUtil = null;
                try {
                    String uploadPath = String.format("%s/%s/%s", bdpJobConfig.getVersion(), bdpVersionProcRel.getProcId(), id);
                    hdfsUtil = new HdfsUtil(bdpJobConfig);
                    hdfsUtil.delete(uploadPath);
                    hdfsUtil.close();
                } catch (Exception e) {
                    if (null != hdfsUtil) {
                        hdfsUtil.close();
                    }
                    log.error("删除版本资源文件:{}异常！error:{}", bdpVersion.getVersionNum(), e.getMessage());
                    throw new RRException("删除版本资源文件:" + bdpVersion.getVersionNum() + "异常！");
                }
            }
        }
    }

    @Override
    public void add(BdpAddVersionDto bdpAddVersionDto) {

        if (null == bdpAddVersionDto || null == bdpAddVersionDto.getProcId()) {
            throw new RRException("项目必须选择！");
        }

        //验证版本号和版本名称的长度20以内以及版本号是否重复
        Integer tenantId = ShiroUtils.getTenantId();
        cheackVersionNumAndName(bdpAddVersionDto.getId(), bdpAddVersionDto.getVersionName(), bdpAddVersionDto.getVersionNum(), tenantId);

        //验证工程和对应的工程下的udf
        cheackVersionUdf(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getUdfList());

        //验证工程和对应的工程下的关联的数据源
        cheackVersionConnect(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getConnectList());

        //验证工程和对应的工程下的数据仓库
        cheackVersionTables(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getTableList());

        //验证工程和对应的工程下的工作流和工作流依赖
        cheackVersionWorkFlow(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getWorkMenuList());

        //验证工程和对应的工程下的实时表
        cheackVersionRealTables(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getRealTableList());

        //验证工程和对应的工程下的实时任务工作流
        cheackVersionRealWorkFlow(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getRealWorkMenuList());


        //验证勾选的作业流依赖中对应的作业流是否全部包含
        cheackVersionWorkFlowInWorkFlowLay(bdpAddVersionDto.getWorkMenuList());


        //保存对应的打包版本记录
        bdpAddVersionDto.setCreateTime(LocalDateTime.now());
        BdpVersion bdpVersion = new BdpVersion();
        BeanUtils.copyProperties(bdpAddVersionDto, bdpVersion);
        bdpVersion.setTenantId(ShiroUtils.getTenantId());
        bdpVersionMapper.insert(bdpVersion);
        //组装数据
        VersionZipDto versionZipDto = null;
        try {
            bdpAddVersionDto.setId(bdpVersion.getId());
            versionZipDto = packZipData(bdpAddVersionDto);
        } catch (Exception e) {
            log.error("组装打包数据异常！error：{}", e.getMessage());
            //删除对应的打包记录
            bdpVersionMapper.deleteById(bdpVersion.getId());
            throw new RRException("组装打包数据失败！error：【" + e.getMessage() + "】");
        }

        //打包失败
        if (null == versionZipDto) {
            //删除对应的打包记录
            bdpVersionMapper.deleteById(bdpVersion.getId());
            throw new RRException("组装打包数据失败！");
        }

        //将对应的数据生成文件保存到hdfs上
        File workFlowFile = null;
        HdfsUtil workHdfsUtil = null;
        try {
            //资源文件
            workHdfsUtil = new HdfsUtil(bdpJobConfig);
            String workFlowPath = String.format("%s/%s/%s", bdpJobConfig.getVersion(), bdpAddVersionDto.getProcId(), versionZipDto.getId());
            if (CompressUtil.createJSONFile(versionZipDto, versionZipDto.getId())) {
                workFlowFile = new File("./" + versionZipDto.getId() + ".json");
                workHdfsUtil.writeFile(FileUtil.readAsByteArray(workFlowFile), workFlowPath);
                workHdfsUtil.close();
            } else {
                if (null != workHdfsUtil) {
                    workHdfsUtil.close();
                }
                throw new RRException("生成文件json文件异常！");
            }
        } catch (Exception e) {
            if (null != workHdfsUtil) {
                workHdfsUtil.close();
            }
            log.error("上传打包的数据异常！error：{}", e.getMessage());
            //删除对应的打包记录
            bdpVersionMapper.deleteById(bdpVersion.getId());
            throw new RRException("上传打包的数据异常！error：【" + e.getMessage() + "】");
        } finally {
            if (null != workFlowFile) {
                if (workFlowFile.isFile()) {
                    workFlowFile.delete();
                }
            }
        }

        //保存打包以后的历史数据记录
        insertIntoVersionProcRel(versionZipDto);

    }

    @Override
    public BdpVersion copyVersion(BdpAddVersionDto bdpAddVersionDto) {
        //保存对应的打包版本记录
        BdpVersion bdpVersion = new BdpVersion();
        BeanUtils.copyProperties(bdpAddVersionDto, bdpVersion);
        bdpVersionMapper.insert(bdpVersion);
        return bdpVersion;
    }

    private void cheackVersionWorkFlowInWorkFlowLay(List<IdAndNameDto> workMenuList) {
        if (CollectionUtil.isNotEmpty(workMenuList)) {
            Set<IdAndNameDto> copWorkList = new HashSet<>(workMenuList);
            Set<Integer> workMenuJob = new HashSet<>();
            Set<WorkMenu> workMenuJobLay = new HashSet<>();
            for (IdAndNameDto idAndNameDto : copWorkList) {
                WorkMenu workMenu = workMenuMapper.selectById(idAndNameDto.getId());
                if (null != workMenu) {
                    if (Objects.equals(workMenu.getWorkType(), WorkFlowMenuType.WORKFLOW_FIELD.getCode())) {
                        WorkMenuJobRel workMenuJobRel = workMenuJobRelMapper.selectOne(new QueryWrapper<WorkMenuJobRel>().eq("work_type", 1).eq("work_menu_id", workMenu.getId()));
                        if (null != workMenuJobRel) {
                            workMenuJob.add(workMenuJobRel.getWorkFlowId().intValue());
                        }
                    }
                    if (Objects.equals(workMenu.getWorkType(), WorkFlowMenuType.WORKFLOW_MGT_FIELD.getCode())) {
                        workMenuJobLay.add(workMenu);
                    }
                }
            }
            //验证勾选的作业流依赖中对应的作业流是否全部包含
            for (WorkMenu workMenu : workMenuJobLay) {
                List<WorkMenuJobRel> workMenuJobRels = workMenuJobRelMapper.selectList(new QueryWrapper<WorkMenuJobRel>().eq("work_type", 2).eq("work_menu_id", workMenu.getId()));
                if (CollectionUtil.isNotEmpty(workMenuJobRels)) {
                    for (WorkMenuJobRel workMenuJobRel : workMenuJobRels) {
                        if (!workMenuJob.contains(workMenuJobRel.getWorkFlowId().intValue())) {
                            JobInfo jobInfo = jobInfoMapper.selectById(workMenuJobRel.getWorkFlowId());
                            String jobName = "";
                            if (null != jobInfo) {
                                jobName = jobInfo.getName();
                            }
                            throw new RRException("作业流依赖:【" + workMenu.getName() + "】中含有未勾选的作业流：【" + jobName + "】");
                        }
                    }
                }
            }
        }
    }

    private void insertIntoVersionProcRel(VersionZipDto versionZipDto) {
        BdpVersionProcRel bdpVersionProcRel = new BdpVersionProcRel();
        bdpVersionProcRel.setVersionId(versionZipDto.getId());
        bdpVersionProcRel.setProcId(versionZipDto.getProcId());
        if (CollectionUtil.isNotEmpty(versionZipDto.getUdfList())) {
            bdpVersionProcRel.setUdfJson(JSONObject.toJSONString(versionZipDto.getUdfList()));
        }
        if (CollectionUtil.isNotEmpty(versionZipDto.getAllUdfList())) {
            bdpVersionProcRel.setOlderAllUdfJson(JSONObject.toJSONString(versionZipDto.getAllUdfList()));
        }
        //离线任务
        if (CollectionUtil.isNotEmpty(versionZipDto.getWorkMenuList())) {
            bdpVersionProcRel.setWorkflowJson(JSONObject.toJSONString(versionZipDto.getWorkMenuList()));
        }
        //实时任务
        if (CollectionUtil.isNotEmpty(versionZipDto.getRealWorkMenuList())) {
            bdpVersionProcRel.setRealWorkflowJson(JSONObject.toJSONString(versionZipDto.getRealWorkMenuList()));
        }
        //老的离线任务菜单数据，方便显示历史数据
        List<WorkMenuDto> workMenuDtos = iworkMenuService.listWorkFlowMenuesNotIncludeNodes(versionZipDto.getProcId());
        if (CollectionUtil.isNotEmpty(workMenuDtos)) {
            bdpVersionProcRel.setOlderAllWorkMenuJson(JSONObject.toJSONString(workMenuDtos));
        }
        //老的实时任务菜单数据，方便显示历史数据
        RealTimeTaskMenuSearchVO param = new RealTimeTaskMenuSearchVO();
        param.setTaskType(1);
        param.setProcId(versionZipDto.getProcId());
        List<RealTimeTaskMenuVO> realWorkMenuDtoList = realTimeTaskMenuService.menuList(param);
        if (CollectionUtil.isNotEmpty(realWorkMenuDtoList)) {
            bdpVersionProcRel.setOlderAllRealWorkMenuJson(JSONObject.toJSONString(realWorkMenuDtoList));
        }
        if (CollectionUtil.isNotEmpty(versionZipDto.getAllConnectList())) {
            bdpVersionProcRel.setOlderAllConnectJson(JSONObject.toJSONString(versionZipDto.getAllConnectList()));
        }
        if (CollectionUtil.isNotEmpty(versionZipDto.getConnectList())) {
            bdpVersionProcRel.setConnectJson(JSONObject.toJSONString(versionZipDto.getConnectList()));
        }
        //所有的离线任务表
        if (CollectionUtil.isNotEmpty(versionZipDto.getAllTableList())) {
            bdpVersionProcRel.setOlderAllTableJson(JSONObject.toJSONString(versionZipDto.getAllTableList()));
        }
        //打包的离线任务表
        if (CollectionUtil.isNotEmpty(versionZipDto.getTableList())) {
            bdpVersionProcRel.setTableJson(JSONObject.toJSONString(versionZipDto.getTableList()));
        }
        //未打包的离线任务菜单
        if (CollectionUtil.isNotEmpty(versionZipDto.getNotCheackWorkMenuList())) {
            bdpVersionProcRel.setNotCheackWorkMenuList(JSONObject.toJSONString(versionZipDto.getNotCheackWorkMenuList()));
        }
        //所有的实时任务表
        if (CollectionUtil.isNotEmpty(versionZipDto.getAllRealTableList())) {
            bdpVersionProcRel.setOlderAllRealTableJson(JSONObject.toJSONString(versionZipDto.getAllRealTableList()));
        }
        //打包的实时任务表
        if (CollectionUtil.isNotEmpty(versionZipDto.getRealTableList())) {
            bdpVersionProcRel.setRealTableJson(JSONObject.toJSONString(versionZipDto.getRealTableList()));
        }
        //未打包的实时任务菜单
        if (CollectionUtil.isNotEmpty(versionZipDto.getNotCheackRealWorkMenuList())) {
            bdpVersionProcRel.setNotCheackRealWorkMenuList(JSONObject.toJSONString(versionZipDto.getNotCheackRealWorkMenuList()));
        }
        bdpVersionProcRelMapper.insert(bdpVersionProcRel);
    }

    /***
     * 组装打包的数据
     * @param bdpAddVersionDto 需要打包的一些数据集合
     * @return
     */
    private VersionZipDto packZipData(BdpAddVersionDto bdpAddVersionDto) {
        //如果是打包，则只管结构，不需要管仓库的数据
        VersionZipDto versionZipDto = new VersionZipDto();
        versionZipDto.setId(bdpAddVersionDto.getId());
        versionZipDto.setVersionName(bdpAddVersionDto.getVersionName());
        versionZipDto.setVersionNum(bdpAddVersionDto.getVersionNum());
        versionZipDto.setVersionDesc(bdpAddVersionDto.getVersionDesc());
        versionZipDto.setCreateTime(bdpAddVersionDto.getCreateTime());
        versionZipDto.setCreatePer(bdpAddVersionDto.getCreatePer());
        versionZipDto.setCreatePerName(sysUserService.getUserAccountByUserId(bdpAddVersionDto.getCreatePer()));
        versionZipDto.setVersionType(bdpAddVersionDto.getVersionType());
        versionZipDto.setProcId(bdpAddVersionDto.getProcId());
        versionZipDto.setZipEnv(SpringContextUtil.getActiveProfile());
        versionZipDto.setZipHost(SpringIpAndHostUtil.getUrl());
        //组装udf函数需要进行打包的内容
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getUdfList())) {
            List<JobRunUdfDto> udfList = packZipUdf(bdpAddVersionDto.getUdfList());
            versionZipDto.setUdfList(udfList);
        }

        //所有的udf集合
        List<JobRunUdf> jobRunUdfs = jobRunUdfMapper.listAllUdf(Long.valueOf(bdpAddVersionDto.getProcId()));
        if (CollectionUtil.isNotEmpty(jobRunUdfs)) {
            List<IdAndNameDto> udfList = new ArrayList<>();
            for (JobRunUdf jobRunUdf : jobRunUdfs) {
                IdAndNameDto idAndNameDto = new IdAndNameDto();
                idAndNameDto.setId(jobRunUdf.getId());
                idAndNameDto.setName(jobRunUdf.getMethodName());
                udfList.add(idAndNameDto);
            }
            List<JobRunUdfDto> newUdfList = packZipUdf(udfList);
            versionZipDto.setAllUdfList(newUdfList);
        }
        //组装数据仓库
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getTableList())) {
            List<TableInfoAndFieldDto> tableList = packZipTable(bdpAddVersionDto.getTableList());
            versionZipDto.setTableList(tableList);
        }
        //所有的数据仓库
        List<TableInfoDto> tableInfos = procTableMapper.listAllTable(Long.valueOf(bdpAddVersionDto.getProcId()));
        if (CollectionUtil.isNotEmpty(tableInfos)) {
            List<IdAndNameDto> tableList = new ArrayList<>();
            for (TableInfoDto tableInfoDto : tableInfos) {
                IdAndNameDto idAndNameDto = new IdAndNameDto();
                idAndNameDto.setId(tableInfoDto.getId());
                idAndNameDto.setName(tableInfoDto.getTableName());
                idAndNameDto.setDbName(tableInfoDto.getDbName());
                tableList.add(idAndNameDto);
            }
            List<TableInfoAndFieldDto> allTableList = packZipTable(tableList);
            versionZipDto.setAllTableList(allTableList);
        }
        //组装实时表
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getRealTableList())) {
            List<RealTableInfoAndFieldDto> realTableList = packZipRealTable(bdpAddVersionDto.getRealTableList());
            versionZipDto.setRealTableList(realTableList);
        }
        //所有的实时表
        List<TableInfoDto> realTableInfos = procTableMapper.listAllRealTable(Long.valueOf(bdpAddVersionDto.getProcId()));
        if (CollectionUtil.isNotEmpty(realTableInfos)) {
            List<IdAndNameDto> realTableList = new ArrayList<>();
            for (TableInfoDto tableInfoDto : realTableInfos) {
                IdAndNameDto idAndNameDto = new IdAndNameDto();
                idAndNameDto.setId(tableInfoDto.getId());
                idAndNameDto.setName(tableInfoDto.getTableName());
                idAndNameDto.setDbName(tableInfoDto.getDbName());
                realTableList.add(idAndNameDto);
            }
            List<RealTableInfoAndFieldDto> allTableList = packZipRealTable(realTableList);
            versionZipDto.setAllRealTableList(allTableList);
        }
        //组装数据源连接
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getConnectList())) {
            List<ConfConnectAndConfigDto> connectList = packZipConnect(bdpAddVersionDto.getConnectList());
            versionZipDto.setConnectList(connectList);
        }
        //所有的数据源连接
        List<ConfConnectDto> olderConnectList = procConnectMapper.listAllConnect(Long.valueOf(bdpAddVersionDto.getProcId()));
        if (CollectionUtil.isNotEmpty(olderConnectList)) {
            List<IdAndNameDto> connectList = new ArrayList<>();
            for (ConfConnectDto confConnectDto : olderConnectList) {
                IdAndNameDto idAndNameDto = new IdAndNameDto();
                idAndNameDto.setId(confConnectDto.getId());
                idAndNameDto.setName(confConnectDto.getName());
                connectList.add(idAndNameDto);
            }
            List<ConfConnectAndConfigDto> allConnectList = packZipConnect(connectList);
            versionZipDto.setAllConnectList(allConnectList);
        }

        //组装离线任务
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getWorkMenuList())) {
            List<WorkMenuAndLayDto> workMenuList = packZipWorkMenu(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getWorkMenuList());
            versionZipDto.setWorkMenuList(workMenuList);
        }
        //组装实时任务
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getRealWorkMenuList())) {
            List<RealTimeTaskMenuAndTaskDto> realWorkMenuList = packZipRealWorkMenu(bdpAddVersionDto.getProcId(), bdpAddVersionDto.getRealWorkMenuList());
            versionZipDto.setRealWorkMenuList(realWorkMenuList);
        }
        //半选的菜单目录
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getNotCheackWorkMenuList())) {
            versionZipDto.setNotCheackWorkMenuList(bdpAddVersionDto.getNotCheackWorkMenuList());
        }
        //半选的实时任务
        if (CollectionUtil.isNotEmpty(bdpAddVersionDto.getNotCheackRealWorkMenuList())) {
            versionZipDto.setNotCheackRealWorkMenuList(bdpAddVersionDto.getNotCheackRealWorkMenuList());
        }
        return versionZipDto;
    }

    private List<WorkMenuAndLayDto> packZipWorkMenu(Integer procId, List<IdAndNameDto> workMenuList) {
        //组装工作流和工作流依赖的资源
        Set<IdAndNameDto> copWorkList = new HashSet<>(workMenuList);
        List<IdAndNameDto> sortList = copWorkList.stream().sorted(Comparator.comparing(IdAndNameDto::getId)).collect(Collectors.toList());
        Set<Integer> workMenus = copWorkList.stream().map(IdAndNameDto::getId).collect(Collectors.toSet());
        List<WorkMenuAndLayDto> list = new ArrayList<>();
        for (IdAndNameDto idAndNameDto : sortList) {
            //查询到对应的菜单信息
            WorkMenu workMenu = workMenuMapper.selectById(idAndNameDto.getId());
            if (null != workMenu) {
                //判断是否没有作业流依赖管理.默认添加一个
                if (Objects.equals(workMenu.getWorkType(), WorkFlowMenuType.SUB_DIR.getCode())) {
                    WorkMenu workMenu1 = workMenuMapper.selectOne(new QueryWrapper<WorkMenu>().eq("work_type", WorkFlowMenuType.WORKFLOW_MGT.getCode()).eq("root_id", workMenu.getRootId()));
                    if (null != workMenu1) {
                        if (!workMenus.contains(workMenu1.getId().intValue())) {
                            workMenus.add(workMenu1.getId().intValue());
                            WorkMenuAndLayDto workMenuAndLayDto = new WorkMenuAndLayDto();
                            BeanUtils.copyProperties(workMenu1, workMenuAndLayDto);
                            workMenuAndLayDto.setCreatePerName(sysUserService.getUserAccountByUserId(workMenuAndLayDto.getCreatePer()));
                            list.add(workMenuAndLayDto);
                        }
                    }
                }
                WorkMenuAndLayDto workMenuAndLayDto = new WorkMenuAndLayDto();
                BeanUtils.copyProperties(workMenu, workMenuAndLayDto);
                workMenuAndLayDto.setCreatePerName(sysUserService.getUserAccountByUserId(workMenuAndLayDto.getCreatePer()));
                if (Objects.equals(workMenuAndLayDto.getWorkType(), WorkFlowMenuType.WORKFLOW_FIELD.getCode())) {
                    //查询到对应的其下的任务和对应的节点和对应的连线和节点的配置属性
                    List<WorkMenuJobRel> workMenuJobRels = workMenuJobRelMapper.selectList(new QueryWrapper<WorkMenuJobRel>()
                            .eq("proc_id", procId).eq("work_menu_id", workMenu.getId()));
                    if (CollectionUtil.isNotEmpty(workMenuJobRels)) {
                        for (WorkMenuJobRel workMenuJobRel : workMenuJobRels) {
                            //查询其下的jobInfo信息
                            if (Objects.equals(1, workMenuJobRel.getWorkType())) {
                                //查询其对应的调度配置信息
                                JobNodeConfig jobConfig = listCronAndSysList(workMenuJobRel.getWorkMenuId());
                                if (null != jobConfig) {
                                    workMenuAndLayDto.setJobNodeConfig(jobConfig);
                                }
                                JobInfo jobInfo = jobInfoMapper.selectById(workMenuJobRel.getWorkFlowId());
                                if (null != jobInfo) {
                                    JobInfoDto jobInfoDto = new JobInfoDto();
                                    BeanUtils.copyProperties(jobInfo, jobInfoDto);
                                    jobInfoDto.setCreatePerName(sysUserService.getUserAccountByUserId(jobInfoDto.getCreatePer()));
                                    //查询其下所有的节点信息
                                    Set<Long> jobIds = new HashSet<>();
                                    jobIds.add(Long.valueOf(jobInfo.getId()));
                                    List<JobNodeInfo> jobNodeInfos = jobNodeInfoMapper.selectListByJobIds(jobIds);
                                    //去掉复制的虚拟的节点
                                    if (CollectionUtil.isNotEmpty(jobNodeInfos)) {
                                        jobNodeInfos = jobNodeInfos.stream().filter(o -> !Objects.equals(1, o.getIsVirCopy())).collect(Collectors.toList());
                                    }
                                    jobInfoDto.setJobNodeInfos(jobNodeInfos);
                                    if (CollectionUtil.isNotEmpty(jobNodeInfos)) {
                                        for (JobNodeInfo jobNodeInfo : jobNodeInfos) {
                                            //查询其下配置的节点的配置信息
                                            List<JobNodeConf> jobNodeConfs = jobNodeConfMapper.selectList(new QueryWrapper<JobNodeConf>().eq("job_type", 0).eq("job_node_id", jobNodeInfo.getId()));
                                            if (CollectionUtil.isNotEmpty(jobNodeConfs)) {
                                                JobNodeConfig jobNodeConfig = listJobNodeConfigList(jobNodeConfs, 0);
                                                if (null != jobNodeConfig) {
                                                    jobNodeInfo.setJobNodeConfig(jobNodeConfig);
                                                }
                                            }
                                            jobNodeInfo.setCreatePerName(sysUserService.getUserAccountByUserId(jobNodeInfo.getCreatePer()));
                                            //查询其对应的节点配置的路由配置信息
                                            List<JobNodeLayConfigDto> reList = jobNodeInfoService.selectJobNodeLayList(jobNodeInfo);
                                            if (CollectionUtil.isNotEmpty(reList)) {
                                                jobNodeInfo.setJobNodeLayConfigDtoList(reList);
                                            }
                                        }
                                    }
                                    workMenuAndLayDto.setJobInfoDto(jobInfoDto);
                                    //对应的依赖连线
                                    List<JobLink> joblinks = jobLinkMapper.selectList(new QueryWrapper<JobLink>().eq("menu_id", workMenu.getId()).eq("linke_type", 0));
                                    if (CollectionUtil.isNotEmpty(joblinks)) {
                                        for (JobLink joblink : joblinks) {
                                            joblink.setCreatePerName(sysUserService.getUserAccountByUserId(joblink.getCreatePer()));
                                        }
                                        workMenuAndLayDto.setJobLinks(joblinks);
                                    }
                                    //查询到对应的质量报警规则关联表信息(data-manger里面)
                                    String baseReqUrl = dataDevelopmentConfig.getDataManagerUrl() + "/bdp/dataManagerQualityRulesRel/listRuleRels?jobId=" + jobInfo.getId();
                                    workMenuAndLayDto.setManagerRulesRelDtos(listRulesRelDtos(baseReqUrl));

                                    //工作流对应的报警规则
                                    List<AlarmObject> alarmObjects = alarmObjectService.listByWorkMenuId(workMenuJobRel.getWorkMenuId());
                                    if (CollectionUtil.isNotEmpty(alarmObjects)) {
                                        workMenuAndLayDto.setAlarmObjects(alarmObjects);
                                    }
                                }
                            }
                        }
                    }
                }
                List<WorkMenuJobRel> workMenuJobRels = new ArrayList<>();
                if (Objects.equals(workMenuAndLayDto.getWorkType(), WorkFlowMenuType.WORKFLOW_MGT_FIELD.getCode())) {
                    workMenuJobRels = workMenuJobRelMapper.selectList(new QueryWrapper<WorkMenuJobRel>()
                            .eq("proc_id", procId)
                            .eq("work_menu_id", workMenu.getId())
                            .eq("work_type", 2));
                    //作业流依赖的一些配置，也是需要配置的
                    //查询其对应的调度配置信息
                    JobNodeConfig jobConfig = listCronAndSysList(workMenu.getId());
                    if (null != jobConfig) {
                        workMenuAndLayDto.setJobNodeConfig(jobConfig);
                    }
                    WorkFlowDep workFlowDep = workFlowDepMapper.selectOne(new QueryWrapper<WorkFlowDep>().eq("work_menu_id", workMenu.getId()));
                    if (null != workFlowDep) {
                        WorkFlowDepDto workFlowDepDto = new WorkFlowDepDto();
                        BeanUtils.copyProperties(workFlowDep, workFlowDepDto);
                        workFlowDepDto.setCreatePerName(sysUserService.getUserAccountByUserId(workFlowDepDto.getCreatePer()));
                        workMenuAndLayDto.setWorkFlowDepDto(workFlowDepDto);
                    }
                    //对应的依赖连线
                    List<JobLink> joblinks = jobLinkMapper.selectList(new QueryWrapper<JobLink>().eq("menu_id", workMenu.getId()).eq("linke_type", 1));
                    if (CollectionUtil.isNotEmpty(joblinks)) {
                        for (JobLink joblink : joblinks) {
                            joblink.setCreatePerName(sysUserService.getUserAccountByUserId(joblink.getCreatePer()));
                        }
                        workMenuAndLayDto.setJobLinks(joblinks);
                    }
                }
                if (CollectionUtil.isNotEmpty(workMenuJobRels)) {
                    workMenuAndLayDto.setWorkMenuJobRels(workMenuJobRels);
                }
                list.add(workMenuAndLayDto);
            }

        }
        if (CollectionUtil.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

    private List<RealTimeTaskMenuAndTaskDto> packZipRealWorkMenu(Integer procId, List<IdAndNameDto> workMenuList) {
        //组装实时任务工作流的资源
        Set<IdAndNameDto> copWorkList = new HashSet<>(workMenuList);
        List<IdAndNameDto> sortList = copWorkList.stream().sorted(Comparator.comparing(IdAndNameDto::getId)).collect(Collectors.toList());
        List<RealTimeTaskMenuAndTaskDto> list = new ArrayList<>();
        for (IdAndNameDto idAndNameDto : sortList) {
            //查询到对应的菜单信息
            RealTimeTaskMenu realTimeTaskMenu = realTimeTaskMenuService.getById(idAndNameDto.getId());
            if (null != realTimeTaskMenu) {
                RealTimeTaskMenuAndTaskDto realTimeTaskMenuAndTaskDto = new RealTimeTaskMenuAndTaskDto();
                BeanUtils.copyProperties(realTimeTaskMenu, realTimeTaskMenuAndTaskDto);
                realTimeTaskMenuAndTaskDto.setCreatePerName(sysUserService.getUserAccountByUserId(realTimeTaskMenuAndTaskDto.getCreatePer()));
                if (Objects.equals(realTimeTaskMenuAndTaskDto.getMenuType(), 2)) {
                    //查询到对应的其下的任务和对应的节点和对应的连线
                    RealTimeTaskVO realTimeTaskVO = realTimeTaskService.getByMenuId(realTimeTaskMenuAndTaskDto.getId());
                    if (realTimeTaskVO != null) {
                        if (Constant.RealTimeTaskType.FLINK.getCode().equals(realTimeTaskVO.getTaskType())) {
                            continue;
                        }
                        realTimeTaskMenuAndTaskDto.setRealTimeTaskVO(realTimeTaskVO);
                        List<FlinkNodeUnion> unionList = flinkNodeUnionMapper.selectList(
                                new QueryWrapper<FlinkNodeUnion>().eq("task_id", realTimeTaskMenuAndTaskDto.getId()));
                        if (CollectionUtils.isNotEmpty(unionList)) {
                            realTimeTaskMenuAndTaskDto.setLineList(unionList);
                        }
                        List<BdpRealTaskAlarmObject> alarmObjects = realTaskAlarmObjectMapper.selectList(new QueryWrapper<BdpRealTaskAlarmObject>().eq("object_id", realTimeTaskMenuAndTaskDto.getId()));
                        if (CollectionUtil.isNotEmpty(alarmObjects)) {
                            realTimeTaskMenuAndTaskDto.setAlarmObjects(alarmObjects);
                        }
                    }
                }
                list.add(realTimeTaskMenuAndTaskDto);
            }

        }
        if (CollectionUtil.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

    @Override
    public List<DataManagerQualityRulesRelDto> listRulesRelDtos(String baseReqUrl) {
//        try {
//            String forObject = restTemplate.getForObject(baseReqUrl, String.class);
//            if(StringUtils.isNotEmpty(forObject)){
//                JSONObject jsonObject = JSONObject.parseObject(forObject);
//                Object data = jsonObject.get("data");
//                if(null != data){
//                    JSONArray jsonArray = JSONArray.parseArray(data.toString());
//                    if(CollectionUtil.isNotEmpty(jsonArray)){
//                        List<DataManagerQualityRulesRelDto> restList = new ArrayList<>();
//                        for (Object o : jsonArray) {
//                            JSONObject obj = JSONObject.parseObject(o.toString());
//                            if(null != obj){
//                                DataManagerQualityRulesRelDto dataManagerQualityRulesRelDto = new DataManagerQualityRulesRelDto();
//                                dataManagerQualityRulesRelDto.setId(obj.getInteger("id"));
//                                dataManagerQualityRulesRelDto.setRuleId(obj.getInteger("ruleId"));
//                                dataManagerQualityRulesRelDto.setJobNodeId(obj.getInteger("jobNodeId"));
//                                dataManagerQualityRulesRelDto.setJobId(obj.getInteger("jobId"));
//                                restList.add(dataManagerQualityRulesRelDto);
//                            }
//                        }
//                        return restList;
//                    }
//                }
//            }
//        }catch (Exception e){
//            log.error("获取质量报警规则异常！error:{}",e.getMessage());
//        }
        return null;
    }

    @Override
    public JobNodeConfig listJobNodeConfigList(List<JobNodeConf> jobNodeConfs, Integer type) {
        if (CollectionUtil.isNotEmpty(jobNodeConfs)) {
            JobNodeConfig jobNodeConfig = new JobNodeConfig();
            if (1 == type) {
                //调度的配置
                return cronConfig(jobNodeConfs);
            }
            JobNodeConfigDto jobNodeConfigDto = jobNodeConfService.getJobNodeConf(jobNodeConfs);
            if (null != jobNodeConfigDto) {
                BeanUtils.copyProperties(jobNodeConfigDto, jobNodeConfig);
            }
            //处理表输出 jobOutPutTableIds---这个需要把对应的库表和id全部列出来
            JobNodeConfig jobNodeConfig1 = JobNodeConfigUtil.changeJobOutPutTableIds(jobNodeConfigDto);
            if (CollectionUtil.isNotEmpty(jobNodeConfig1.getJobOutPutTableIds())) {
                List<String> outPutDatas = new ArrayList<>();
                for (String tableId : jobNodeConfig1.getJobOutPutTableIds()) {
                    TableInfo tableInfo = null;
                    try {
                        tableInfo = tableInfoMapper.selectById(Integer.valueOf(tableId.trim()));
                    } catch (Exception e) {
                        log.error("输出仓库的表id:{}解析异常，error:{}", tableId, e.getMessage());
                    }
                    String dbAndName = "";
                    if (null != tableInfo) {
                        dbAndName = tableInfo.getDbName() + "." + tableInfo.getTableName();
                    }
                    outPutDatas.add(dbAndName);
                }
                jobNodeConfig.setJobOutPutTableNames(outPutDatas);
            }
            jobNodeConfig.setJobOutPutTableIds(jobNodeConfig1.getJobOutPutTableIds());
            //SAP输出表数据
            JobNodeConfig jobNodeConfig2 = JobNodeConfigUtil.changeSapOutPutTableName(jobNodeConfigDto);
            jobNodeConfig.setSap_output_table_name(jobNodeConfig2.getSap_output_table_name());

            SapConnectField sapConnectField = JobNodeConfigUtil.changeSapConnectFields(jobNodeConfigDto);
            if (null != sapConnectField) {
                jobNodeConfig.setSapConnectFieldInfo(sapConnectField);
            }

            return jobNodeConfig;
        }
        return null;
    }


    private JobNodeConfig listCronAndSysList(Long workMenuId) {
        List<JobNodeConf> jobNodeConfs = jobNodeConfMapper.selectList(new QueryWrapper<JobNodeConf>().eq("job_type", 1).eq("job_node_id", workMenuId));
        return cronConfig(jobNodeConfs);
    }

    @Override
    public JobNodeConfig cronConfig(List<JobNodeConf> jobNodeConfs) {
        if (CollectionUtil.isNotEmpty(jobNodeConfs)) {
            JobNodeConfig jobNodeConfig = new JobNodeConfig();
            for (JobNodeConf jobNodeConf : jobNodeConfs) {
                //得到这个节点的数据结构，并且并且重新赋值
                if ("sysParam".equals(jobNodeConf.getKey())) {
                    if (null != jobNodeConf.getValue()) {
                        JSONArray jsonArray = JSONArray.parseArray(jobNodeConf.getValue());
                        jobNodeConf.setValue(JSONArray.toJSONString(jsonArray));
                        List<Map<String, Object>> sysParam = new ArrayList<>();
                        for (Object o : jsonArray) {
                            if (null != o) {
                                JSONObject obj = JSONObject.parseObject(o.toString());
                                if (null != obj) {
                                    Map<String, Object> innerMap = obj.getInnerMap();
                                    sysParam.add(innerMap);
                                }
                            }
                        }
                        jobNodeConfig.setSysParam(sysParam);
                    }
                }
                //新的运行参数的结构
                if ("nodeParam".equals(jobNodeConf.getKey())) {
                    if (null != jobNodeConf.getValue()) {
                        JSONArray jsonArray = JSONArray.parseArray(jobNodeConf.getValue());
                        if (CollectionUtil.isNotEmpty(jsonArray)) {
                            List<JSONObject> ll = new ArrayList<>();
                            for (Object o : jsonArray) {
                                ll.add(JSONObject.parseObject(o.toString()));
                            }
                            jobNodeConfig.setNodeParam(ll);
                        }
                    }
                }
                if ("cronType".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setCronType(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("cronUrl".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setCronUrl(jobNodeConf.getValue());
                    }
                }
                if ("scheduleOrInterval".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setScheduleOrInterval(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("jobNodeId".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setJobNodeId(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("jobType".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setJobType(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("schedule_type".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setSchedule_type(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("expression".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setExpression(jobNodeConf.getValue());
                    }
                }
                if ("isRetry".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setIsRetry(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("reTryMax".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setReTryMax(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("reTryInterval".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setReTryInterval(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("isDurTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setIsDurTime(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("startTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setStartTime(jobNodeConf.getValue());
                    }
                }
                if ("endTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setEndTime(jobNodeConf.getValue());
                    }
                }

                if ("isOpenTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setIsOpenTime(Integer.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("sysDurTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setSysDurTime(Double.valueOf(jobNodeConf.getValue()));
                    }
                }
                if ("sysEndTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setSysEndTime(jobNodeConf.getValue());
                    }
                }
                if ("sysStartTime".equals(jobNodeConf.getKey())) {
                    if (StringUtils.isNotEmpty(jobNodeConf.getValue())) {
                        jobNodeConfig.setSysStartTime(jobNodeConf.getValue());
                    }
                }
            }
            return jobNodeConfig;
        }
        return null;
    }

    private List<ConfConnectAndConfigDto> packZipConnect(List<IdAndNameDto> connectList) {
        //查询到对应的关联的连接信息
        if (CollectionUtil.isNotEmpty(connectList)) {
            List<ConfConnectAndConfigDto> restList = new ArrayList<>();
            for (IdAndNameDto idAndNameDto : connectList) {
                ConfConnect confConnect = confConnectMapper.selectById(idAndNameDto.getId());
                if (null != confConnect) {
                    ConfConnectAndConfigDto confConnectAndConfigDto = new ConfConnectAndConfigDto();
                    BeanUtils.copyProperties(confConnect, confConnectAndConfigDto);
                    confConnectAndConfigDto.setCreatePerName(sysUserService.getUserAccountByUserId(confConnectAndConfigDto.getCreatePer()));
                    if (Objects.equals(ConnectTypeEnum.Sap.getCode(), confConnect.getTypeId())) {
                        SapConnectField sapConnectField = sapConnectFieldMapper.selectOne(new QueryWrapper<SapConnectField>().eq("connect_id", idAndNameDto.getId()));
                        if (null != sapConnectField) {
                            confConnectAndConfigDto.setSapConnectFieldInfo(sapConnectField);
                        }
                    }
//                    if(Objects.equals(ConnectTypeEnum.JieKou.getCode(),confConnect.getTypeId())){
                    List<ConnectFieldInfo> connectFieldInfos = connectFieldInfoMapper.selectList(new QueryWrapper<ConnectFieldInfo>().eq("connect_id", idAndNameDto.getId()));
                    if (CollectionUtil.isNotEmpty(connectFieldInfos)) {
                        confConnectAndConfigDto.setConnectFieldInfos(connectFieldInfos);
                    }
//                    }
                    restList.add(confConnectAndConfigDto);
                }
            }
            return restList;
        }
        return null;
    }

    private List<TableInfoAndFieldDto> packZipTable(List<IdAndNameDto> tableList) {
        if (CollectionUtil.isNotEmpty(tableList)) {
            List<TableInfoAndFieldDto> restList = new ArrayList<>();
            for (IdAndNameDto idAndNameDto : tableList) {
                TableInfo tableInfo = tableInfoMapper.selectById(idAndNameDto.getId());
                if (null != tableInfo) {
                    TableInfoAndFieldDto tableInfoAndFieldDto = new TableInfoAndFieldDto();
                    BeanUtils.copyProperties(tableInfo, tableInfoAndFieldDto);
                    tableInfoAndFieldDto.setCreatePerName(sysUserService.getUserAccountByUserId(tableInfoAndFieldDto.getCreatePer()));
                    //查询其下的字段
                    List<TableFieldInfo> tableFieldInfos = tableFieldInfoMapper.selectList(new QueryWrapper<TableFieldInfo>().eq("table_id", idAndNameDto.getId()));
                    if (CollectionUtil.isNotEmpty(tableFieldInfos)) {
                        tableInfoAndFieldDto.setTableFieldInfos(tableFieldInfos);
                    }
                    restList.add(tableInfoAndFieldDto);
                }
            }
            return restList;
        }
        return null;
    }

    private List<RealTableInfoAndFieldDto> packZipRealTable(List<IdAndNameDto> tableList) {
        if (CollectionUtil.isNotEmpty(tableList)) {
            List<RealTableInfoAndFieldDto> restList = new ArrayList<>();
            for (IdAndNameDto idAndNameDto : tableList) {
                BdpRealTableInfo tableInfo = realTableInfoMapper.selectById(idAndNameDto.getId());
                if (null != tableInfo) {
                    RealTableInfoAndFieldDto tableInfoAndFieldDto = new RealTableInfoAndFieldDto();
                    BeanUtils.copyProperties(tableInfo, tableInfoAndFieldDto);
                    tableInfoAndFieldDto.setCreatePerName(sysUserService.getUserAccountByUserId(tableInfoAndFieldDto.getCreateUser().intValue()));
                    //查询其下的字段
                    List<BdpRealTableField> tableFieldInfos = realTableFieldMapper.selectList(new QueryWrapper<BdpRealTableField>().eq("table_id", idAndNameDto.getId()));
                    if (CollectionUtil.isNotEmpty(tableFieldInfos)) {
                        tableInfoAndFieldDto.setTableFieldInfos(tableFieldInfos);
                    }
                    restList.add(tableInfoAndFieldDto);
                }
            }
            return restList;
        }
        return null;
    }

    private List<JobRunUdfDto> packZipUdf(List<IdAndNameDto> udfList) {
        if (CollectionUtil.isNotEmpty(udfList)) {
            List<JobRunUdfDto> jobRunUdfDtos = new ArrayList<>();
            for (IdAndNameDto idAndNameDto : udfList) {
                JobRunUdf jobRunUdf = jobRunUdfMapper.selectById(idAndNameDto.getId());
                if (null != jobRunUdf) {
                    JobRunUdfDto jobRunUdfDto = new JobRunUdfDto();
                    BeanUtils.copyProperties(jobRunUdf, jobRunUdfDto);
                    jobRunUdfDto.setCreatePerName(sysUserService.getUserAccountByUserId(jobRunUdfDto.getCreaterPerId()));
                    jobRunUdfDtos.add(jobRunUdfDto);
                }
            }
            return jobRunUdfDtos;
        }

        return null;
    }


    private void cheackVersionWorkFlow(Integer procId, List<IdAndNameDto> workMenuList) {
        if (CollectionUtil.isNotEmpty(workMenuList)) {
            for (IdAndNameDto idAndNameDto : workMenuList) {
                WorkMenu workMenu = workMenuMapper.selectOne(new QueryWrapper<WorkMenu>().eq("proc_id", procId).eq("id", idAndNameDto.getId()));
                if (null == workMenu) {
                    throw new RRException("项目的菜单目录：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionRealWorkFlow(Integer procId, List<IdAndNameDto> realWorkMenuList) {
        if (CollectionUtil.isNotEmpty(realWorkMenuList)) {
            for (IdAndNameDto idAndNameDto : realWorkMenuList) {
                List<RealTimeTaskMenu> realTimeTaskMenus = realTimeTaskMenuService.list(new QueryWrapper<RealTimeTaskMenu>().eq("proc_id", procId).eq("id", idAndNameDto.getId()));
                if (CollectionUtil.isEmpty(realTimeTaskMenus)) {
                    throw new RRException("项目的实时任务菜单目录：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionTables(Integer procId, List<IdAndNameDto> tableList) {
        if (CollectionUtil.isNotEmpty(tableList)) {
            for (IdAndNameDto idAndNameDto : tableList) {
                ProcTable procTable = procTableMapper.selectOne(new QueryWrapper<ProcTable>().eq("proc_id", procId).eq("table_id", idAndNameDto.getId()).eq("table_type", 1));
                if (null == procTable) {
                    throw new RRException("项目关联的数据仓库：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionRealTables(Integer procId, List<IdAndNameDto> realTableList) {
        if (CollectionUtil.isNotEmpty(realTableList)) {
            for (IdAndNameDto idAndNameDto : realTableList) {
                ProcTable procTable = procTableMapper.selectOne(new QueryWrapper<ProcTable>().eq("proc_id", procId).eq("table_id", idAndNameDto.getId()).eq("table_type", 2));
                if (null == procTable) {
                    throw new RRException("项目关联的实时表：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionConnect(Integer procId, List<IdAndNameDto> connectList) {
        if (CollectionUtil.isNotEmpty(connectList)) {
            //查询这个工程关联的数据源连接
            for (IdAndNameDto idAndNameDto : connectList) {
                ProcConnect procConnect = procConnectMapper.selectOne(new QueryWrapper<ProcConnect>().eq("proc_id", procId).eq("connect_id", idAndNameDto.getId()));
                if (null == procConnect) {
                    throw new RRException("项目关联的数据源：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionUdf(Integer procId, List<IdAndNameDto> udfList) {
        if (CollectionUtil.isNotEmpty(udfList)) {
            //查询到这个工程下所有的udf函数
            for (IdAndNameDto idAndNameDto : udfList) {
                JobRunUdf jobRunUdf = jobRunUdfMapper.selectOne(new QueryWrapper<JobRunUdf>().eq("proc_id", procId).eq("id", idAndNameDto.getId()));
                if (null == jobRunUdf) {
                    throw new RRException("项目的udf函数：【" + idAndNameDto.getName() + "】不存在！");
                }
            }
        }
    }

    private void cheackVersionNumAndName(Long id, String versionName, String versionNum, Integer tenantId) {
        if (StringUtils.isEmpty(versionName)) {
            throw new RRException("版本名称必填！");
        }
        if (versionName.length() > 20) {
            throw new RRException("版本名称长于20！");
        }
        if (StringUtils.isEmpty(versionNum)) {
            throw new RRException("版本号必填！");
        }
        if (versionNum.length() > 20) {
            throw new RRException("版本号长于20！");
        }
        List<BdpVersion> bdpVersions = bdpVersionMapper.selectList(new QueryWrapper<BdpVersion>()
                .eq("version_num", versionNum)
                .eq(null != tenantId, "tenant_id", tenantId)
                .ne(null != id, "id", id));
        if (CollectionUtil.isNotEmpty(bdpVersions)) {
            throw new RRException("版本号重复！");
        }
    }

    @Override
    public BdpAddVersionDto view(Long id) {
        BdpVersion bdpVersion = bdpVersionMapper.selectById(id);
        if (null == bdpVersion) {
            throw new RRException("版本不存在！");
        }
        //查询到版本对应的信息
        BdpVersionProcRel bdpVersionProcRel = bdpVersionProcRelMapper.selectOne(new QueryWrapper<BdpVersionProcRel>().eq("version_id", id));
        if (null == bdpVersionProcRel) {
            throw new RRException("版本资源不存在！");
        }
        //查询对应的资源信息
        BdpAddVersionDto bdpAddVersionDto = new BdpAddVersionDto();
        BeanUtils.copyProperties(bdpVersion, bdpAddVersionDto);
        bdpAddVersionDto.setProcId(bdpVersionProcRel.getProcId());
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getTableJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getTableJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<IdAndNameDto> tableList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            IdAndNameDto idAndNameDto = new IdAndNameDto();
                            idAndNameDto.setId(jsonObject.getInteger("id"));
                            idAndNameDto.setName(jsonObject.getString("tableName"));
                            tableList.add(idAndNameDto);
                        }
                    }
                }
                bdpAddVersionDto.setTableList(tableList);
            }
        }
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getRealTableJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getRealTableJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<IdAndNameDto> realTableList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            IdAndNameDto idAndNameDto = new IdAndNameDto();
                            idAndNameDto.setId(jsonObject.getInteger("id"));
                            idAndNameDto.setName(jsonObject.getString("tableName"));
                            realTableList.add(idAndNameDto);
                        }
                    }
                }
                bdpAddVersionDto.setRealTableList(realTableList);
            }
        }
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getUdfJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getUdfJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<IdAndNameDto> tableList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            IdAndNameDto idAndNameDto = new IdAndNameDto();
                            idAndNameDto.setId(jsonObject.getInteger("id"));
                            idAndNameDto.setName(jsonObject.getString("jarMethodName"));
                            tableList.add(idAndNameDto);
                        }
                    }
                }
                bdpAddVersionDto.setUdfList(tableList);
            }
        }
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getConnectJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getConnectJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<IdAndNameDto> tableList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            IdAndNameDto idAndNameDto = new IdAndNameDto();
                            idAndNameDto.setId(jsonObject.getInteger("id"));
                            idAndNameDto.setName(jsonObject.getString("name"));
                            tableList.add(idAndNameDto);
                        }
                    }
                }
                bdpAddVersionDto.setConnectList(tableList);
            }
        }
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getWorkflowJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getWorkflowJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<Integer> notCheacks = new ArrayList<>();
                //半选的菜单id
                if (StringUtils.isNotEmpty(bdpVersionProcRel.getNotCheackWorkMenuList())) {
                    JSONArray jsonArray1 = JSONArray.parseArray(bdpVersionProcRel.getNotCheackWorkMenuList());
                    if (CollectionUtil.isNotEmpty(jsonArray1)) {
                        for (Object o : jsonArray1) {
                            if (null != o) {
                                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                                if (null != jsonObject) {
                                    notCheacks.add(jsonObject.getInteger("id"));
                                }
                            }
                        }
                    }
                }
                List<IdAndNameDto> tableList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            if (!notCheacks.contains(jsonObject.getInteger("id"))) {
                                IdAndNameDto idAndNameDto = new IdAndNameDto();
                                idAndNameDto.setId(jsonObject.getInteger("id"));
                                idAndNameDto.setName(jsonObject.getString("name"));
                                tableList.add(idAndNameDto);
                            }
                        }
                    }
                }
                bdpAddVersionDto.setWorkMenuList(tableList);
            }
        }
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getRealWorkflowJson())) {
            JSONArray jsonArray = JSONArray.parseArray(bdpVersionProcRel.getRealWorkflowJson());
            if (CollectionUtil.isNotEmpty(jsonArray)) {
                List<Integer> notCheacks = new ArrayList<>();
                //半选的菜单id
                if (StringUtils.isNotEmpty(bdpVersionProcRel.getNotCheackRealWorkMenuList())) {
                    JSONArray jsonArray1 = JSONArray.parseArray(bdpVersionProcRel.getNotCheackRealWorkMenuList());
                    if (CollectionUtil.isNotEmpty(jsonArray1)) {
                        for (Object o : jsonArray1) {
                            if (null != o) {
                                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                                if (null != jsonObject) {
                                    notCheacks.add(jsonObject.getInteger("id"));
                                }
                            }
                        }
                    }
                }
                List<IdAndNameDto> realWorkMenuList = new ArrayList<>();
                for (Object o : jsonArray) {
                    if (null != o) {
                        JSONObject jsonObject = JSONObject.parseObject(o.toString());
                        if (null != jsonObject) {
                            if (!notCheacks.contains(jsonObject.getInteger("id"))) {
                                IdAndNameDto idAndNameDto = new IdAndNameDto();
                                idAndNameDto.setId(jsonObject.getInteger("id"));
                                idAndNameDto.setName(jsonObject.getString("name"));
                                realWorkMenuList.add(idAndNameDto);
                            }
                        }
                    }
                }
                bdpAddVersionDto.setRealWorkMenuList(realWorkMenuList);
            }
        }

        return bdpAddVersionDto;
    }

    @Override
    public ProcRelDto resourceList(Long procId) {
        //查询到工程的信息
        ProcInfo procInfo = procInfoMapper.selectById(procId);
        if (null == procInfo) {
            throw new RRException("工程不存在！");
        }
        ProcRelDto procRelDto = new ProcRelDto();
        procRelDto.setProcId(procInfo.getId());
        procRelDto.setProcName(procInfo.getName());
        procRelDto.setCreatePer(procInfo.getCreatePer());
        //根据工程id查询对应的关联的资源数据
        List<ConfConnectDto> connectList = procConnectMapper.listAllConnect(procId);
        List<JobRunUdf> jobRunUdfs = jobRunUdfMapper.listAllUdf(procId);
        List<TableInfoDto> tableInfos = procTableMapper.listAllTable(procId);
        List<TableInfoDto> realTableInfos = procTableMapper.listAllRealTable(procId);
        List<WorkMenuDto> workMenuDtos = iworkMenuService.listWorkFlowMenuesNotIncludeNodes(procId.intValue());
        RealTimeTaskMenuSearchVO param = new RealTimeTaskMenuSearchVO();
        param.setTaskType(1);
        param.setProcId(procId.intValue());
        List<RealTimeTaskMenuVO> realWorkMenuDtoList = realTimeTaskMenuService.menuList(param);
        if (CollectionUtil.isNotEmpty(workMenuDtos)) {
            procRelDto.setWorkMenuDtoList(workMenuDtos);
        }
        if (CollectionUtil.isNotEmpty(realWorkMenuDtoList)) {
            procRelDto.setRealWorkMenuDtoList(realWorkMenuDtoList);
        }

        if (CollectionUtil.isNotEmpty(connectList)) {
            procRelDto.setConnectList(connectList);
        }
        if (CollectionUtil.isNotEmpty(jobRunUdfs)) {
            procRelDto.setUdfList(jobRunUdfs);
        }
        if (CollectionUtil.isNotEmpty(tableInfos)) {
            procRelDto.setTableList(tableInfos);
        }
        if (CollectionUtil.isNotEmpty(realTableInfos)) {
            procRelDto.setRealTableList(realTableInfos);
        }
        return procRelDto;
    }

    @Override
    public void downZipVersion(Long versionId, HttpServletResponse response, HttpServletRequest request) throws Exception {
        //查询对应的版本的文件资源，并且压缩成文件，并且下载下来
        BdpVersion bdpVersion = bdpVersionMapper.selectById(versionId);
        if (null == bdpVersion) {
            throw new RRException("版本不存在！");
        }
        //查询到版本对应的信息
        BdpVersionProcRel bdpVersionProcRel = bdpVersionProcRelMapper.selectOne(new QueryWrapper<BdpVersionProcRel>().eq("version_id", versionId));
        if (null == bdpVersionProcRel) {
            throw new RRException("版本资源不存在！");
        }
        //资源文件
        List<File> files = new ArrayList<>();
        HdfsUtil workHdfsUtil = new HdfsUtil(bdpJobConfig);
        String workFlowPath = String.format("%s/%s/%s", bdpJobConfig.getVersion(), bdpVersionProcRel.getProcId(), bdpVersion.getId());
        FSDataInputStream fsDataInputStream = workHdfsUtil.readFile(workFlowPath);
        if (null != fsDataInputStream) {
            File tempFileJson = File.createTempFile("tempJson" + UUID.randomUUID(), ".json");
            OutputStream os = new FileOutputStream(tempFileJson);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = fsDataInputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            fsDataInputStream.close();
            files.add(tempFileJson);
            os.close();
            if (null != workHdfsUtil) {
                workHdfsUtil.close();
            }
        }
        if (null != workHdfsUtil) {
            workHdfsUtil.close();
        }
        //下载资源
        String zipName = bdpVersion.getVersionName() + ".zip";
        CompressUtil.setDownloadResponse(response, zipName, request);
        CompressUtil.compressZip(files, response.getOutputStream());
    }

    @Override
    public ProcRelStrDto olderresourceList(Long versionId) {
        //查询到对应的历史配置数据
        BdpVersion bdpVersion = bdpVersionMapper.selectById(versionId);
        if (null == bdpVersion) {
            throw new RRException("当前版本不存在！");
        }
        BdpVersionProcRel bdpVersionProcRel = bdpVersionProcRelMapper.selectOne(new QueryWrapper<BdpVersionProcRel>().eq("version_id", bdpVersion.getId()));
        if (null == bdpVersionProcRel) {
            throw new RRException("当前版本包为空数据！");
        }
        //查询到工程的信息
        ProcInfo procInfo = procInfoMapper.selectById(bdpVersionProcRel.getProcId());
        if (null == procInfo) {
            throw new RRException("工程不存在！");
        }
        //转换对象
        ProcRelStrDto procRelDto = new ProcRelStrDto();
        procRelDto.setProcId(bdpVersionProcRel.getProcId());
        procRelDto.setProcName(procInfo.getName());
        procRelDto.setCreatePer(procInfo.getCreatePer());
        String olderAllUdfJson = bdpVersionProcRel.getOlderAllUdfJson();
        //udf的数据
        if (StringUtils.isNotEmpty(olderAllUdfJson)) {
            procRelDto.setUdfList(olderAllUdfJson);
        }
        //数据源连接
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getOlderAllConnectJson())) {
            procRelDto.setConnectList(bdpVersionProcRel.getOlderAllConnectJson());
        }
        //数据仓库
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getOlderAllTableJson())) {
            procRelDto.setTableList(bdpVersionProcRel.getOlderAllTableJson());
        }
        //菜单树
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getOlderAllWorkMenuJson())) {
            procRelDto.setWorkMenuDtoList(bdpVersionProcRel.getOlderAllWorkMenuJson());
        }
        //实时表
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getOlderAllRealTableJson())) {
            procRelDto.setRealTableList(bdpVersionProcRel.getOlderAllRealTableJson());
        }
        //实时任务菜单
        if (StringUtils.isNotEmpty(bdpVersionProcRel.getOlderAllRealWorkMenuJson())) {
            procRelDto.setRealWorkMenuDtoList(bdpVersionProcRel.getOlderAllRealWorkMenuJson());
        }

        return procRelDto;
    }


}
